<?php
class Customer_model extends MY_Model{

	function __construct()
	{
		parent::__construct();
		$this->_table		=	'customers';
		$this->primary_key	=	'id';
	}
	/**
	 * Check customer
	 * @param email
	 */
	function check_customer($email='')
	{
		$this->db->where('email', $email);
		$this->db->select('COUNT(*) as total');
		$result = $this->db->get($this->_table)->row_array();
		if($result['total'] > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Liet ke danh sach cac order
	 */
	function list_all_customer($start=0, $limit=10, $whe=array())
	{
		$this->db->limit($limit, $start);
		$this->db->select('id, title, fullname, email, country, telephone, active, last_ip');
		$this->db->order_by("id DESC");
		$this->db->where('is_del', 'no');
		$res = $this->db->get($this->_table)->result_array();
		return $res;
	}
	
	/**
	 * Tong so customer
	 */
	
	function total_all_customer($stt=array())
	{
		$this->db->where('is_del', 'no');
		$res = $this->db->select("COUNT(id) as total")->get($this->_table)->row_array();
		return $res["total"];
	}
	
	/**
	 * Tong so customer active
	 */
	function total_active_customer($whe=array())
	{
		$this->db->where('is_del', 'no');
		$this->db->where('active', 'yes');
		$res = $this->db->select("COUNT(id) as total")->get($this->_table)->row_array();
		return $res["total"];
	}
	
	/**
	 * Edit customer
	 */
	function get_customer_by_id($cus_id)
	{
		$this->db->where("id", $cus_id);
		return $this->db->get($this->_table)->row_array();
	}
	/**
	 * Update customer
	 */
	function customer_update($id, $data)
	{
		$this->db->where("id", $id);
		$this->db->update($this->_table, $data);
	}
	
	/**
	 * Delete customer
	 */
	function customer_delete($id)
	{
		$this->db->where("id", $id);
		$this->db->update($this->_table, array('is_del'=>'yes'));
	}
	
	function customer_create($data)
	{
		$this->db->insert($this->_table, $data);
	}
}